package com.site.service.impl.backend.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.site.dao.backend.system.AuthDao;
import com.site.dto.backend.system.AuthorityBizBean;
import com.site.model.backend.system.AuthorityInfo;
import com.site.service.backend.system.AuthService;
import com.site.util.PropertyUtils;
import com.site.util.StringUtils;
import com.site.util.base.service.BaseService;
import com.site.util.base.service.BaseServiceImpl;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

@Component("authService")
public class AuthServiceImpl extends BaseServiceImpl implements AuthService {

	private AuthDao authDao;
	
	@Override
	public List<AuthorityBizBean> getAuthList(QueryPage queryPage, AuthorityBizBean authorityBizBean) {
		
		List<AuthorityBizBean> voList = new ArrayList<AuthorityBizBean>();
		List<AuthorityInfo> poList = authDao.getAuthList(queryPage, authorityBizBean);
		if(null != poList){
			AuthorityBizBean vo = null;
			for (AuthorityInfo authorityInfo : poList) {
				if(null != authorityInfo){
				vo = new AuthorityBizBean();
					PropertyUtils.copyProperties(vo, authorityInfo);
					voList.add(vo);
				}
			}
		}
		return voList;
	}

	@Override
	public Object doExecute(Object obj, String command) throws SystemException,
			ApplicationException {
		if(command.equals(BaseService.COMMAND_SAVE)){
			save((AuthorityBizBean)obj);
		} else if(command.equals(BaseService.COMMAND_UPDATE)){
			update((AuthorityBizBean)obj);
		} else if(command.equals("disabled")){
			disabled((String)obj);
		} else if(command.equals("enabled")){
			enabled((String)obj);
		} else if(COMMAND_DELETE.equals(command)){
			//物理删除
			return authDelete((List<Long>)obj);
		}
		return null;
	}
	
	/**
	 * 物理删除权限
	 * @param obj
	 */
	private String authDelete(List<Long> obj) {
		StringBuffer sb = new StringBuffer();
		for (Long long1 : obj) {
			AuthorityInfo po = null;
			try {
				if(null != long1){
					po = authDao.getEntityById(AuthorityInfo.class, long1);
					authDao.delete(AuthorityInfo.class, long1);
					if(null != po){
						sb.append(po.getName());
						sb.append(";");
					}
				}
			} catch (Exception e) {
				log.info(e.getMessage());
				if(null != po){
					sb.append(po.getName());
					sb.append(";");
				}
			}
		}
		
		if(!StringUtils.isEmpty(sb.toString())){
			log.debug(sb.toString());
			return StringUtils.removeLastComma2(sb.toString());
		}
		
		return null;
	}

	/**
	 * 删除(禁用)权限
	 * @param obj
	 */
	private void disabled(String obj) {
		if(null != obj && obj.length() > 0){
			authDao.disabled(obj);
		}
	}
	/**
	 * 更新权限角色
	 * @param roleBizBean
	 */
	private void update(AuthorityBizBean authorityBizBean) {
		if(null != authorityBizBean){
			authDao.update(authorityBizBean);
		} else {
			log.info("权限对象为空了,不能执行更新操作!");
		}
	}
	/**
	 * 启用权限
	 * @param obj
	 */
	private void enabled(String obj) {
		if(null != obj && obj.length() > 0){
			authDao.enabled(obj);
		}
	}

	/**
	 * 保存权限
	 * @param obj
	 */
	private void save(AuthorityBizBean obj) {
		if(null != obj){
			authDao.save(obj);
		} else {
			log.info("权限Id为空了,不能执行保存操作!");
		}
	}

	@Resource
	public void setAuthDao(AuthDao authDao) {
		this.authDao = authDao;
	}

	public AuthDao getAuthDao() {
		return authDao;
	}

	@Override
	public AuthorityBizBean getAuthByName(String name) {
		AuthorityInfo authorityInfo = this.authDao.getAuthByName(name);
		AuthorityBizBean authorityBizBean = null;
		if(null != authorityInfo){
			authorityBizBean = new AuthorityBizBean();
			PropertyUtils.copyProperties(authorityBizBean, authorityInfo);
		}
		return authorityBizBean;
	}

	@Override
	public List<AuthorityBizBean> getNotRoleAuth(Long roleId,AuthorityBizBean vo, QueryPage queryPage) {
		List<AuthorityBizBean> authorityBizBeanList = null;
		List<AuthorityInfo> authorityInfoList = authDao.getNotRoleAuthList(roleId, vo, queryPage);
		if(null != authorityInfoList && authorityInfoList.size() > 0){
			AuthorityBizBean authorityBizBean = null;
			authorityBizBeanList = new ArrayList<AuthorityBizBean>();
			for (AuthorityInfo authorityInfo : authorityInfoList) {
				if(null != authorityInfo){
					authorityBizBean = new AuthorityBizBean();
					PropertyUtils.copyProperties(authorityBizBean, authorityInfo);
					authorityBizBeanList.add(authorityBizBean);
				}
			}
		}
		return authorityBizBeanList;
	}

}
